const Router = require("koa2-router");
const mockjs = require("mockjs");
const { data } = require("../../uni_modules/uview-ui/libs/mixin/mixin");
const {
	placeOrders
} = require("./controller/pay");
const router = new Router();
const arr = [{
	username: 'admin',
	passwrok: '123'
}];
router.get("/", async (ctx) => {
	ctx.body = {
		code: 200,
		message: "接口服务正常！",
		data: [{
			title: "下单接口",
			url: "/pay/placeOrder",
			methods: "POST",
			params: {
				openid: "获取支付用户openid",
				total: "total：支付金额1代表1分",
				description: "支付商品描述",
				out_trade_no: "商户订单号",
			},
		}, ],
	};
});

router.post("/pay/placeOrder", placeOrders);
//数据接口
router.post('/api/users', ctx => {
	let list = mockjs.mock({
		'data|15': [{
			'ceci|1-200': 1,
			'money|1-1000': 100,
			'state|1': ['无票', '可抢票', '已停售'],
			'dates|0-23': 1,
			'datev|0-59': 1,
			'dates1|0-23': 1,
			'datev1|0-59': 1,
			'hours|0-23': 1,
			'minute|0-59': 1,
		}]
	})
	ctx.body = {
		message: list
	};
});
//登录接口
router.post('/api/denglu', ctx => {
	//获取前端数据
	let objName = ctx.request.body;
	//当msg存在走登录接口否则注册
	if (objName.msg) {
		//判断前端账号密码是否存在
		let pass = arr.some(item => item.username == objName.msg.username)
		//为真时执行
		if (pass) {
			arr.forEach(item => {
				//判断账号
				if (item.username == objName.msg.username) {
					//判断密码
					if (item.passwrok == objName.msg.passwrok) {
						//当expor为真进行判断为token失效时间接口
						if(objName.msg.exports){
							let guotime=objName.msg.timese
							// 获取当前时间
							let showTime=new Date().getTime();
							//判断间隔是否大于失效时间
							if(showTime - guotime >objName.msg.exports*1){
								ctx.body = {
									message: '已失效',
								};
							}else{
								ctx.body = {
									message: '未过期',
								};
							}

						}else{
							let main=new Date().getTime()
							//登陆成功返回提示
							ctx.body = {
								message: '登陆成功',
								//过期时间设置（1天）
								exports:86400,
								//获取登录时间戳
								timese:main
							};
					
						}
					} else {
						//密码错误返回
						ctx.body = {
							message: '密码错误',
						};
					}
				}
			})
		} else {
			//用户名不存在返回
			ctx.body = {
				message: '用户名不存在',
			};
		}
	} else {
		//注册接口
		arr.push(objName.message)
		ctx.body = {
			message: '注册成功',
		};
	}

});

module.exports = router;